built in statements: bound function
bound function:
description:
foo {var|one>, var|two>, ...} #=>
statements ...
a type of function that binds variables before the invocation of the function body
basically, what every other language already does!
after return from the function, the bound variables are returned to their original definition
the input sequence is always assigned to "input |seq>"
if it only has one parameter, the function can also be invoked as a simple operator
the ket/superposition/sequence in the last line of the function body is the returned variable
examples:
-- define a simple function that outputs its input variables:
foo {var|one>, var|two>} #=>
sprint["input: "] input|seq>
sprint["one: "] var|one>
sprint["two: "] var|two>
|return var>
-- now invoke it:
foo(ssplit |ab>, ssplit |uv>) ssplit |xyz>
input: |x> . |y> . |z>
one: |a> . |b>
two: |u> . |v>
|return var>
-- define a one parameter bound function:
bah {var|one>} #=>
sprint["input: "] input|seq>
sprint["one: "] var|one>
|return var>
-- now invoke it, first as a function:
bah(ssplit|abc>) ssplit |xyz>
input: |x> . |y> . |z>
one: |a> . |b> . |c>
|return var>
-- now invoke it as a simple operator:
-- note in this case input|seq> is not defined
bah ssplit |abc>
input: |>
one: |a> . |b> . |c>
|return var>
see also:
if, for, op-for, op-sfor, while, test-bound-functions.sw3
Home